<?php
/**
 * @file name : setting.php
 * @package   :
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
 if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Setting extends Base_Controller
{
	/**
	 * __construct
	 * @access public
	 * @param
	 * @return
	 */
	public function __construct()
	{
		parent::__construct();
	}
	/**
	  * _remap
	  * @access public
	  * @param
	  * @return
	  */
	public function _remap( $method )
	{
		switch ($method) {

			case 'save':
				$this->saveEdit();
				break;

			default:
				$this->index();
		}
	}
	/**
	 * index
	 */
	private function index()
	{
	    $controlName    = $this->input->get_post('c');
	    //$this->load->model('Users_model', 'users_model');
        /**
         * Get message when save insert or edit submit
         *
         */
        $msgBoxDisp = '';
        $clsType    = '';
        $clsIcon    = '';

        $messages = $this->getErrMsg($msgBoxDisp, $clsType, $clsIcon);

	    /**
	     * Load user info
	     */
	    $userInfo = $this->users_model->loadUserinfo();
	    $data = array('userInfo'     => $userInfo,
	                  'actionForm'   => '?c=' . $controlName . '&m=save',
                      'messages'        => $messages,
                      'clsType'         => $clsType,
                      'clsIcon'         => $clsIcon,
                      'msgBoxDisp'      => $msgBoxDisp);
	    $this->msmarty->view($controlName . '/admin.user_setting', $data);
	}
	/**
	 * saveEdit
	 *
	 */
	private function saveEdit()
	{
	    //$this->load->model('Users_model', 'users_model');
	    if ($_SERVER['REQUEST_METHOD']==='POST') {
	        $access    = $this->input->post('access_name');
	        $fullName  = $this->input->post('f_name');
	        $email     = $this->input->post('email');
	        $oldPwd    = $this->input->post('old_pwd');
	        $newPwd    = $this->input->post('new_pwd');
	        $retypePwd = $this->input->post('retype_pwd');
            //Load lang msg
            $this->lang->load('success', $this->lang_tag);
            $this->lang->load('error', $this->lang_tag);

            $errMsg = array();
	        if (!preg_match('/^[a-zA-Z0-9]{5,16}$/', $access)) {
                array_push($errMsg, $this->lang->line('setting_access_char'));
	        }

	        if (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)) {
                array_push($errMsg, $this->lang->line('setting_email_invalid'));
	        }
	        if (strlen($newPwd) < 6) {
	            array_push($errMsg, $this->lang->line('setting_pwd_less_char'));
	        }
	        if (strcmp($newPwd, $retypePwd) != 0) {
	            array_push($errMsg, $this->lang->line('setting_pwd_not_match'));
	        }
            $userInfo = $this->users_model->loadUserinfo();
            if (strcmp(dohash($oldPwd, 'md5'), $userInfo->user_password) != 0) {
                array_push($errMsg, $this->lang->line('setting_old_pwd_invalid'));
            }
	        if (count($errMsg) > 0) {
                foreach ($errMsg as $k => $msg) {
                    $this->messages->add($msg, 'error');
                }
                $this->cateHomePage('setting');
                return FALSE;
            }
            $data['user_name']     = $fullName;
            $data['user_access']   = $access;
            $data['user_email']    = $email;
            $data['user_password'] = dohash($newPwd, 'md5');
            $data['user_id']       = 1;
			///// Start transaction /////
			/////////////////////////////
			$this->db->trans_begin();
			$ret = $this->users_model->storeDB($data, 'user_id');
			if ($ret == FALSE) {
			    // Rollback transaction
			    $this->db->trans_rollback();
			    $this->messages->add($this->lang->line('db_insert_update_err'), 'error');
			    $this->cateHomePage('setting');
                return FALSE;
			}
            $this->messages->add($this->lang->line('db_update_success'), 'success');
	    }
	    $this->cateHomePage('setting');
	}
    /**
     * @function getErrMsg
     *
     * @author   Vo Thanh Minh <vothanhminh@yahoo.com - vothanhminh@gmail.com>
     * @access   private
     * @param    string $msgBoxDisp, $clsType, $clsIcon
     * @return   array
     */
    private function getErrMsg(&$msgBoxDisp, &$clsType, &$clsIcon)
    {
        $messages = $this->messages->get();

        if (!is_array($messages)) {
            $messages = array();
            $msgBoxDisp = 'none;';
        } else {
            foreach($messages as $type => $message) {
                if(!empty($message)) {
                    if ($type == 'error') {
                        $clsType = $type;
                        $clsIcon = 'errorIcon';
                    } else if ($type == 'success') {
                        $clsType = $type;
                        $clsIcon = 'successIcon';
                    }
                    break;
                }
            }
        }
        $this->messages->clear();
        return $messages;
    }
	/**
	  * cateHomePage
	  * @access private
	  * @param
	  * @return
	  */
	private function cateHomePage($controlName)
	{
		header("Location:?c=" . $controlName);
	}
}